﻿@using admin.Model
@{
    ViewData["Title"] = "BookList";
    Layout = "~/Views/Shared/_SubMenuLayout.cshtml";
    var book = Model as List<Book>;
}


<nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 图书管理 <span class="c-gray en">&gt;</span> 图书列表 <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新"><i class="Hui-iconfont">&#xe68f;</i></a></nav>
<div class="page-container">
    <div class="cl pd-5 bg-1 bk-gray mt-20">
        <span class="l">
            <a href="javascript:;" onclick="Book_batch_del('/Home/BookBatchDel')" class="btn btn-danger radius"><i class="Hui-iconfont">&#xe6e2;</i> 批量删除</a>
            <a class="btn btn-primary radius" onclick="Book_add('添加图书','/Home/BookAdd')" href="javascript:;"><i class="Hui-iconfont">&#xe600;</i> 添加图书</a>
        </span>
        <span class="r">共有数据：<strong>@book.Count </strong> 条</span>
    </div>
    <div class="mt-20">
        <table class="table table-border table-bordered table-bg table-hover table-sort">
            <thead>
                <tr class="text-c">
                    <th width="4%"><input name="" type="checkbox" value=""></th>
                    <th width="8%">图书ID</th>
                    <th width="">图书名</th>
                    <th width="15%">ISBN</th>
                    <th width="15%">类型</th>
                    <th width="10%">单价(元/本)</th>
                    <th width="10%">库存量(本)</th>
                    <th width="10%">总销售量(本)</th>
                    <th width="10%">操作</th>
                </tr>
            </thead>
            <tbody>
                @for (int i = 0; i < book.Count; i++)
                {
                    <tr class="text-c va-m">
                        <td><input name="bookChk" type="checkbox" value="@book[i].Id"></td>
                        <td class="td-status">@book[i].Id</td>
                        <td><a title="查看图书信息" onClick="Book_show('查看图书信息','/Home/BookShow',@book[i].Id)" href="javascript:;">@book[i].Name</a></td>
                        <td class="text-c lb-number">@book[i].Isbn</td>
                        <td >@book[i].Type</td>
                        <td><span class="price">@book[i].Price</span> </td>
                        <td ><span class="text-c lb-number">@book[i].Inventory</span></td>
                        <td ><span class="text-c lb-number">@book[i].Countsale</span></td>
                        <td class="td-manage">
                            <a style="text-decoration:none" class="ml-5" onClick="Book_edit('编辑图书信息','/Home/BookEdit',@book[i].Id)" href="javascript:;" title="编辑"><i class="Hui-iconfont">&#xe60c;</i></a>
                            <a style="text-decoration:none" class="ml-5" onClick="Book_del(this,'/Home/BookDel','@book[i].Id')" href="javascript:;" title="删除"><i class="Hui-iconfont">&#xe6e2;</i></a>
                        </td>
                    </tr>
                }

            </tbody>
        </table>
    </div>
</div>

<script type="text/javascript">

    /*图书-删除方法*/
    function Book_del(obj, url, id) {
        url = url + '?id=' + id;
        layer.confirm('确认要删除吗？', function (index) {
            $.ajax({
                type: 'POST',
                url: url,
                dataType: 'json',
                success: function (data) {
                    if (data == true) {
                        $(obj).parents("tr").remove();
                        layer.msg('已删除!', { icon: 1, time: 4000 });
                    }
                    else if (data == false) {
                        layer.msg('删除失败了! 请稍后再试', { icon: 5, time: 1000 });
                    }
                },
                error: function (data) {
                    layer.msg('删除失败了! 请稍后再试', { icon: 5, time: 1000 });
                },
            });
        });
    }

    /*图书-批删除方法*/
    function Book_batch_del(url) {
        var checkedNum = $("input[name='bookChk']:checked").length;
        if (checkedNum == 0) {
            layer.msg('请选择至少一项！', { icon: 1, time: 4000 });
            return;
        }
        layer.confirm('确定要删除所选项目？', function (index) {
            var checkedList = new Array();
            $("input[name='bookChk']:checked").each(function () {
                checkedList.push($(this).val());
            });
            $.ajax({
                type: "POST",
                url: url,
                data: { 'delitems': checkedList.toString() },
                success: function (result) {
                    if (result == true) {
                        $("input[name='bookchk']:checked").each(function () {
                            $(this).parent("tr").remove();
                        });
                        $("[name ='bookChk']:checkbox").attr("checked", false);
                        layer.msg('已删除所选项!', { icon: 1, time: 3000 });
                        self.setInterval(function () {  // 这个方法是说在延迟两秒后执行大括号里的方法
                            window.location.reload();   // 这个方法是刷新当前页面
                        }, 2000); //这里2000代表两秒
                    }
                    else if (result == false) {
                        layer.msg('删除失败! 所选图书存在不可删除项', { icon: 5, time: 3000 });
                    }
                },
                error: function (response) {
                    layer.msg('删除失败! 所选用户存在未完成的订单', { icon: 5, time: 3000 });
                },
            });
        });
    }
    /*图书-添加界面*/
    function Book_add(title, url) {
        var index = layer.open({
            type: 2,
            title: title,
            content: url
        });
        layer.full(index);
    }

    /*图书-查看界面*/
    function Book_show(title, url, id) {
        var url = url + '?id=' + id;
        var index = layer.open({
            type: 2,
            title: title,
            content: url
        });
        layer.full(index);
    }
    /*图书-编辑界面*/
    function Book_edit(title, url, id) {
        var url = url + '?id=' + id;
        var index = layer.open({
            type: 2,
            title: title,
            content: url
        });
        layer.full(index);
    }

 

</script>